#include "light.h"

Light::Light(const GLenum& number)
: ambient(4)
, diffuse(4)
, specular(4)
, position(4, 0)
, number(number)
{
	float amb[] = {0.2, 0.2, 0.2, 0.0};
	float dif[] = {0.8, 0.8, 0.8, 1.0};
	float spec[] = {1.0, 1.0, 1.0, 1.0};

	for(int i = 0; i != 4; ++i)
	{
		ambient[i] = amb[i];
		diffuse[i] = dif[i];
		specular[i] = spec[i];
	}

	position[0] = 10.0;
	position[1] = 20.0;
	position[2] = 20.0;
}

void Light::applyLight()
{
	glLightfv(number, GL_AMBIENT, &ambient[0]);
	glLightfv(number, GL_DIFFUSE, &diffuse[0]);
	glLightfv(number, GL_SPECULAR, &specular[0]);
	glLightfv(number, GL_POSITION, &position[0]);
}

void Light::applyPosition()
{
	glLightfv(number, GL_POSITION, &position[0]);
}

void Light::getLightMatrices(std::vector<float>& projection, std::vector<float>& modelview) const
{
	projection.resize(16);
	modelview.resize(16);

	glPushMatrix();
		glPushMatrix();
			glLoadIdentity();
			gluLookAt(position[0], position[1], position[2],
				0,	0,	0, 0, 1, 0);
			glGetFloatv(GL_MODELVIEW_MATRIX, &modelview[0]);
		glPopMatrix();
		glLoadIdentity();
		glOrtho(-20.0, 20.0, -10.0, 10.0, 0, 100);
		glGetFloatv(GL_MODELVIEW_MATRIX, &projection[0]);
	glPopMatrix();
}